﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Benefit.Models.View;
namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员月累计
    /// </summary>
    public class ServiceOperatorMonthSum : Benefit.Interface.View.IOperatorMonthSum
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorMonthSum(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public void InitOperatorMonthSum(int month, int year)
        {
            List<Models.Sys.Operator> operators = db.Operators.ToList();
            foreach (Models.Sys.Operator op in operators)
            {
                InitOperatorMonthSum(op.Id, month, year);
            }
            db.SaveChanges();
        }

        public void Delete(int month, int year)
        {
            var query = db.OperatorMonthSun.Where(a => a.Month == month).Where(a => a.Year == year).ToList();
            foreach (Models.View.OperatorMonthSum s in query)
            {
                db.OperatorMonthSun.Remove(s);
            }
            db.SaveChanges();
        }

        public Models.View.OperatorMonthSum GetOperatorMonthSum(int operatorid, int month, int year)
        {
            var query = db.OperatorMonthSun.Where(a => a.OperatorId == operatorid).Where(a => a.Year == year).Where(a => a.Month == month).Include(a => a.Operator).ToList();
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }

        public List<Models.View.OperatorMonthSum> GetOperatorMonthSums(int month, int year)
        {
            var query = db.OperatorMonthSun.Where(a => a.Year == year).Where(a => a.Month == month).Include(a => a.Operator).ToList();
            return query;
        }
        /// <summary>
        /// 获取操作员月总值(这个方法应该分两部分，如果账户这个月没有换过操作员，直接用账户总值，否则，计算每天的总值)
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public void InitOperatorMonthSum(int operatorid, int month, int year)
        {
            var queryExist = GetOperatorMonthSum(operatorid, month, year);

            OperatorMonthSum change = new OperatorMonthSum();
            change.Year = year;
            change.Month = month;
            change.OperatorId = operatorid;
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            //Sys.TradeHistory th = new Sys.TradeHistory();
            Models.Sys.TradeHistory th = ith.GetMonthLastTradingDay(year, month);
            if (th.MPdate != 0)
            {
                List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(th.Id), operatorid);
                if (accounts.Count > 0)
                {
                    foreach (Models.Sys.Account account in accounts)
                    {
                        bool hasChangedOwner = iacount.HasChangedOwnerInMonth(year, month, account.Id);
                        if (hasChangedOwner)//换过用户，那么从每天的交易情况累计总值
                        {
                            var query = from t in db.AccountDayChange where t.AccountId == account.Id && t.TradeHistory.YPdate == year && t.TradeHistory.MPdate == month select t;
                            foreach (Models.Day.AccountDayChange ch in query)
                            {
                                change.BillCount += ch.BillCount;
                                change.DayCount += ch.DayCount;
                                change.Free += ch.Free;
                                change.LoseCount += ch.LoseCount;
                                change.OperatorId = operatorid;
                                change.Profit += ch.Profit;
                                change.SumLost += ch.SumLost;
                                change.SumWin += ch.SumWin;
                                change.UsedMargin += ch.UsedMargin;
                                change.WinCount += ch.WinCount;
                            }

                            var _query = from t in db.AccountDayAnalysis where t.AccountId == account.Id && t.TradeHistory.YPdate == year && t.TradeHistory.MPdate == month select t;
                            foreach (Models.Day.AccountDayAnalysis ch in _query)
                            {
                                change.AvgBillBenefit += ch.AvgBillBenefit;
                                change.AvgPostionTime += ch.AvgPostionTime;
                                change.MaxReturn += ch.MaxReturn;
                                change.OverStopLoss += ch.OverStopLoss;
                                change.PingJinPing += ch.PingJinPing;
                                change.RaiseRate += ch.RaiseRate;
                                change.RiskCount += ch.RiskCount;
                                change.Utilization += ch.Utilization;
                                change.WinRate += ch.WinRate;
                                change.Yield += ch.Yield;
                            }
                        }
                        else//没有换过用户，那么直接累加账户的月总值
                        {
                            var query = from t in db.AccountMonthChange where t.AccountId == account.Id && t.Month == th.MPdate && t.Year == th.YPdate select t;
                            if (query.Count() > 0)
                            {
                                change.BillCount += query.First().BillCount;
                                change.DayCount += query.First().DayCount;
                                change.Free += query.First().Free;
                                change.LoseCount += query.First().LoseCount;
                                change.OperatorId = operatorid;
                                change.Profit += query.First().Profit;
                                change.SumLost += query.First().SumLost;
                                change.SumWin += query.First().SumWin;
                                change.UsedMargin += query.First().UsedMargin;
                                change.WinCount += query.First().WinCount;
                            }


                            var _query = from t in db.AccountMonthAnalysis where t.AccountId == account.Id && t.Month == th.MPdate && t.Year == th.YPdate select t;
                            if (query.Count() > 0)
                            {
                                change.AvgBillBenefit += _query.First().AvgBillBenefit;
                                change.AvgPostionTime += _query.First().AvgPostionTime;
                                change.MaxReturn += _query.First().MaxReturn;
                                change.OverStopLoss += _query.First().OverStopLoss;
                                change.PingJinPing += _query.First().PingJinPing;
                                change.RaiseRate += _query.First().RaiseRate;
                                change.RiskCount += _query.First().RiskCount;
                                change.Utilization += _query.First().Utilization;
                                change.WinRate += _query.First().WinRate;
                                change.Yield += _query.First().Yield;
                            }
                        }


                    }
                }
            }
            if (queryExist != null)
            {
                queryExist.AvgBillBenefit = change.AvgBillBenefit;
                queryExist.AvgPostionTime = change.AvgPostionTime;
                queryExist.BillCount = change.BillCount;
                queryExist.DayCount = change.DayCount;
                queryExist.Free = change.Free;
                queryExist.LoseCount = change.LoseCount;
                queryExist.MaxReturn = change.MaxReturn;
                queryExist.OverStopLoss = change.OverStopLoss;
                queryExist.PingJinPing = change.PingJinPing;
                queryExist.Profit = change.Profit;
                queryExist.RaiseRate = change.RaiseRate;
                queryExist.RiskCount = change.RiskCount;
                queryExist.SumLost = change.SumLost;
                queryExist.SumWin = change.SumWin;
                queryExist.UsedMargin = change.UsedMargin;
                queryExist.Utilization = change.Utilization;
                queryExist.WinCount = change.WinCount;
                queryExist.WinRate = change.WinRate;
                queryExist.Yield = change.Yield;
            }
            else
            {
                db.OperatorMonthSun.Add(change);
            }
        }

        public List<OperatorDayChange> GetOperatorDayChanges(int month, int year, int operatorid)
        {
            var query = db.OperatorDayChange.Where(a => a.TradeHistory.MPdate == month).Where(a => a.TradeHistory.YPdate == year).Where(a => a.OperatorId == operatorid).ToList();
            return query;
        }


        public List<Models.View.OperatorMonthSum> GetGroupOperatorDayChange(int groupId, int month, int year)
        {
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IDayGroupOperators idgo = new Sys.ServiceDayGroupOperators(db);
            Interface.View.IOperatorMonthSum ioms = new View.ServiceOperatorMonthSum(db);
            //Sys.TradeHistory th = new Sys.TradeHistory();
            Models.Sys.TradeHistory th = ith.GetMonthLastTradingDay(year, month);
            List<Models.Sys.Operator> operators = idgo.GetGroupOperators(groupId, Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate));
            List<Models.View.OperatorMonthSum> changes = new List<Models.View.OperatorMonthSum>();
            foreach (Models.Sys.Operator op in operators)
            {
                Models.View.OperatorMonthSum change = ioms.GetOperatorMonthSum(op.Id, th.MPdate, th.YPdate);
                changes.Add(change);
            }
            return changes;
        }
    }
}
